/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shop.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.thinkgem.jeesite.common.persistence.DataEntity;

/**
 * 商品规格管理Entity
 * @author xxs
 * @version 2016-03-03
 */
public class SpecificationItem extends DataEntity<SpecificationItem> {
	
	private static final long serialVersionUID = 1L;
	private Specification shopSpecification;		// 业务主表ID 父类
	private String name;		// 名称
	
	private List<SpecificationItem.Entry> entries = new ArrayList<SpecificationItem.Entry>();
	
	public SpecificationItem() {
		super();
	}

	public SpecificationItem(String id){
		super(id);
	}

	public SpecificationItem(Specification shopSpecification){
		this.shopSpecification = shopSpecification;
	}

	@Length(min=0, max=64, message="业务主表ID长度必须介于 0 和 64 之间")
	public Specification getShopSpecification() {
		return shopSpecification;
	}

	public void setShopSpecification(Specification shopSpecification) {
		this.shopSpecification = shopSpecification;
	}
	
	@Length(min=0, max=100, message="名称长度必须介于 0 和 100 之间")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Valid
	@NotEmpty
	public List<SpecificationItem.Entry> getEntries() {
		return entries;
	}

	public void setEntries(List<SpecificationItem.Entry> entries) {
		this.entries = entries;
	}

	@JsonIgnore
	public boolean isSelected() {
		if (CollectionUtils.isNotEmpty(getEntries())) {
			for (SpecificationItem.Entry entry : getEntries()) {
				if (entry.getIsSelected()) {
					return true;
				}
			}
		}
		return false;
	}

	public boolean isValid(SpecificationValue specificationValue) {
		if (specificationValue != null && specificationValue.getId() != null && StringUtils.isNotEmpty(specificationValue.getValue()) && CollectionUtils.isNotEmpty(getEntries())) {
			for (SpecificationItem.Entry entry : getEntries()) {
				if (entry != null && entry.getIsSelected() && specificationValue.getId().equals(entry.getId()) && StringUtils.equals(entry.getValue(), specificationValue.getValue())) {
					return true;
				}
			}
		}
		return false;
	}

	public static class Entry implements Serializable {

		private static final long serialVersionUID = 3450488045828163309L;

		private String id;

		private String value;
		
		private Boolean isSelected;

		@NotNull
		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		@NotEmpty
		@Length(max = 200)
		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}

		@NotNull
		public Boolean getIsSelected() {
			return isSelected;
		}

		public void setIsSelected(Boolean isSelected) {
			this.isSelected = isSelected;
		}

	}
	
}